use sea_orm::{DatabaseConnection, DatabaseTransaction, query::*,};
use axum::Json;
use serde_json::Value;
use crate::code;

use super::connect;

// 事务开始
pub async fn begin(conn:DatabaseConnection) -> Result<DatabaseTransaction, Json<Value>> {
    match conn.begin().await {
        Ok(txn) => Ok(txn),
        Err(e) => Err(code::sys_("MySQL 数据库事务开始出错：".to_string() + &e.to_string()))
    }
}
pub async fn start() -> Result<DatabaseTransaction, Json<Value>> {
    let conn = connect().await?;
    match conn.begin().await {
        Ok(txn) => Ok(txn),
        Err(e) => Err(code::sys_("MySQL 数据库事务开始出错：".to_string() + &e.to_string()))
    }
}
// 事务回滚
pub async fn commit(txn:DatabaseTransaction) -> Result<Json<Value>, Json<Value>> {
    match txn.commit().await {
        Ok(_) => Ok(code::ok_()),
        Err(e) => Err(code::sys_("MySQL 数据库事务回滚出错：".to_string() + &e.to_string()))
    }
}